//Francisco "Kiko" Olvera
//Controls all game logic
//contains one board and four players
//Controller.h

#include "Controller.h"
#include <stdio.h>

void Controller::Init(HWND hwnd, HINSTANCE hInst)
{
	blank.loadTexture("Hidden_Item.png");
	blank.setScale(0.25f);
	sprite.Init();
	input.init(hwnd, hInst);
	state = MENU;
	menu.Init(&sprite, &input);
}

void Controller::Update()
{
	input.Update();

	CONTROLLER_STATE newState;

	switch(state)
	{
	case MENU:		newState = menu.Update();		break;
	case GAME:		newState = game.Update();		break;
	case END_GAME:	newState = endGame.Update();	break;
	};

	if(newState != state)
	{
		switch(state)
		{
		case MENU:		menu.Release();		break;
		case GAME:		game.Release();		break;
		case END_GAME:	endGame.Release();	break;
		};
		state = newState;
		switch(state)
		{
		case MENU:		menu.Init(&sprite, &input);							break;
		case GAME:		game.Init(&sprite, &input, numPlayers, players);	break;
		case END_GAME:	endGame.Init(&sprite, &input, winningPlayer);		break;
		};
	}
}

void Controller::Render()
{	switch(state)
	{
	case MENU:		menu.Render();		break;
	case GAME:		game.Render();		break;
	case END_GAME:	endGame.Render();	break;
	};
}

Board* Controller::getBoard()
{
	return game.getBoard();
}

void Controller::setPlayers(int* playerArray)
{
	numPlayers = playerArray[0];
	players = new int[numPlayers];
	for(int i=0;i<numPlayers;++i)
	{
		players[i] = playerArray[i+1];
	}
}

Player* Controller::getPlayers()
{
	return game.getPlayers();
}

char* Controller::getWinningPlayerPic()
{
	int win = players[winningPlayer];
	sprintf_s(buffer, 64, "Player%i.png", win + 1);
	return buffer;
}

int Controller::getNumPlayers()
{
	return numPlayers;
}

void Controller::setWinningPlayer(int player)
{
	winningPlayer = player;
}

int Controller::CorrectPlayerSound(int player, int sound)
{
	int correctSound = players[player] * NUM_PLAYER_SOUNDS;
	correctSound += sound;
	return correctSound;
}